package com.learnjava.dao.impl;

import com.learnjava.bean.Info;
import com.learnjava.dao.InfoDao;
import com.learnjava.util.DBUtil;
import org.apache.commons.lang.StringUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author: xiangty
 * @Date: 2018-03-30
 */
public class InfoDaoImpl extends DBUtil implements InfoDao {
    @Override
    public List<Info> getAll() {
        List<Info> infolist = new ArrayList<Info>();
        String sql = "select * from info";
        Info info = null;
        rs = commQuery(sql);
        try {
            while (rs.next()) {
                info = new Info();
                info.setId(rs.getInt("id"));
                info.setUsername(rs.getString("username"));
                info.setPassword(rs.getString("password"));
                infolist.add(info);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return infolist;
    }

    @Override
    public List<Info> getInfoByUsername(String username) {
        if (StringUtils.isBlank(username)) {
            return null;
        }

        List<Info> infolist = new ArrayList<Info>();
        String sql = "select * from info where username=?";
        Info info = null;
        rs = commQuery(sql, username);
        try {
            while (rs.next()) {
                info = new Info();
                info.setId(rs.getInt("id"));
                info.setUsername(rs.getString("username"));
                info.setPassword(rs.getString("password"));
                infolist.add(info);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return infolist;
    }

    @Override
    public Integer addInfo(Info info) {
        String sql = "insert info(username,password) values(?,?)";
        return commUpdate(sql, info.getUsername(), info.getPassword());
    }


}
